<template>
  <a-modal
    title="交租"
    :width="600"
    class="model-yz"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
          <a-form-item v-show="false"><a-input v-decorator="['id']"/></a-form-item>
          <a-form-item v-show="false"><a-input v-decorator="['ownerLeaseId']"/></a-form-item>
          <a-form-item
              label="付款日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
            <a-date-picker style="width: 100%" placeholder="请选择付款日期" v-decorator="['receiveDate', {rules: [{ required: true, message: '请选择付款日期' }]}]" @change="onChangemeterReadingDate"/>
          </a-form-item>
          <a-form-item
              label="收款方式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-select style="width: 100%" placeholder="请选择收款方式" v-decorator="['collectionType', {rules: [{ required: true, message: '请选择收款方式！' }]}]">
                <a-select-option v-for="(item,index) in collectionTypeData" :key="index" :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
          </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { ownerLeaseBillTzQsEdit } from '@/api/modular/main/ownerleasebill/ownerLeaseBillManage'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        receiveDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        collectionTypeData: []
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (id, ownerLeaseId) {
        this.visible = true
        this.initNeedData()
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: id,
              ownerLeaseId: ownerLeaseId,
              receiveDate: moment(moment().format('YYYY-MM-DD'))
            }
          )
        }, 100)
        this.receiveDateDateString = moment().format('YYYY-MM-DD')
      },
      initNeedData() {
        busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.receiveDate = this.receiveDateDateString
            ownerLeaseBillTzQsEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('退租清算成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.msg)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangemeterReadingDate(date, dateString) {
        this.receiveDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
<style lang="less" scoped>
</style>
